<template>
  <div class="login-container">
    <!-- 中心 -->
    <div class="login-form">
      <!-- logo图标 -->
      <img
        src="http://likede2-admin.itheima.net/img/logo.595745bd.png"
        alt=""
        class="login-logo" />
      <!-- 登录表单 -->
      <el-form
        ref="from"
        :rules="rules"
        :model="from">
        <!-- 用户名 -->
        <el-form-item prop="loginName">
          <el-input
            placeholder="请输入用户名"
            v-model="from.loginName"
            prefix-icon="el-icon-mobile-phone"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="from.password"
            :type="iconshow ? '' : 'password'"
            prefix-icon="el-icon-lock">
            <template #suffix>
              <!-- 点击睁眼闭眼 -->
              <div
                class="icon"
                @click="show">
                <i :class="iconshow ? 'iconfont icon-view' : 'iconfont icon-no-view'"></i>
              </div>
            </template>
          </el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-input
            v-model="from.code"
            placeholder="请输入验证码">
            <template #prefix>
              <div class="icon">
                <i class="iconfont icon-security"></i>
              </div>
            </template>
          </el-input>
          <!-- 点击事件获取新的验证码 -->
          <div
            class="imgcode"
            @click="getRandom()">
            <!-- <img :src="imgCode" alt="" /> -->
            <!-- 将随机token拼接到路径后面得到验证码图片 -->
            <img
              :src="
                '//likede2-admin.itheima.net/likede/api/user-service/user/imageCode/' + from.clientToken
              "
              alt="加载失败" />
          </div>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-button
          :loading="loading"
          type="primary"
          class="btn"
          @click="submit"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
// 引入外部CSS
import '@/styles/login.scss'
// 引入随机token
import { clientToken } from '@/utils/random'
// 引入登录接口
import { login } from '@/api/user'
export default {
  async created() {
    // 初始随机验证码
    this.getRandom()
  },
  data() {
    return {
      loading: false,
      iconshow: false,
      imgCode: '',
      from: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        loginType: 0,
        clientToken: '',
      },
      // 验证规则
      rules: {
        loginName: [
          {
            required: true,
            message: '用户名不能为空',
            trigger: ['blur', 'change'],
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change'],
          },
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: ['blur', 'change'],
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change'],
          },
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空',
            trigger: ['blur', 'change'],
          },
        ],
      },
    }
  },
  methods: {
    // 随机验证码
    getRandom() {
      this.from.clientToken = clientToken()
    },
    async submit() {
      // 将登录转为加载中，防止多次点击
      this.loading = true
      // 对整个表单进行验证
      await this.$refs.from.validate()
      // 发送登录请求
      try {
        const res = await login(this.from)
        // 判断验证码是否输入正确
        if (res.data.success) {
          this.$store.commit('user/setStateToken', res.data)
          this.$router.push('/')
        }
        // 验证码错误让用户再次登录
        this.loading = false
        // 更换验证码
        this.getRandom()
      } catch (error) {
        // 登陆失败重置，让用户再次登录
        this.loading = false
      }
    },
    // 点击睁眼闭眼
    show() {
      this.iconshow = !this.iconshow
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-input__inner {
  height: 52px;
}
</style>
